﻿using NCP.Infrastructure.Common;

namespace System.Collections.Generic
{
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// 集合的深度拷贝（列表的每项都将创建为新项）
        /// </summary>
        /// <typeparam name="TEntity">拷贝的类型</typeparam>
        /// <param name="collention">目标列表</param>
        /// <returns>拷贝后的列表</returns>
        public static List<TTo> DepthCopy<TFrom, TTo>(this IEnumerable<TFrom> collention,
            Action<TFrom,TTo> action = null)
            where TFrom : class, new()
            where TTo : class, new()
        {
            var copyCollection = new List<TTo>();
            if (collention == null)
            {
                return copyCollection;
            }

            foreach (var fromEntity in collention)
            {
                copyCollection.Add(fromEntity.CopyItem<TFrom, TTo>(action));
            }
            return copyCollection;
        }
    }
}
